#include <stdio.h>
#include <assert.h>

#include <SDL2/SDL.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/samplefmt.h>
#include <libavutil/imgutils.h>

#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIO_FRAME_SIZE 192000
typedef struct PacketQueue
{
  AVPacketList *first_pkt, *last_pkt;
  int nb_packets;
  int size;
  SDL_mutex *mutex;
  SDL_cond *cond;
} PacketQueue;

PacketQueue audioq;
PacketQueue videoq;

int quit = 0;
struct SwrContext *audio_convert_ctx = NULL;
static Uint8 out_channel = 2;
static SDL_AudioFormat out_format = AV_SAMPLE_FMT_S16;
static int out_nb_samples = 0;
static int sample_rate = 48000;
static enum AVPixelFormat out_yuv_foramt = AV_PIX_FMT_YUV420P;
static AVFrame *yuv_frame;
static struct SwsContext *sws_ctx = NULL;
static int w_width = 640;
static int w_height = 480;
static SDL_Window *win;
static SDL_Renderer *renderer;
static SDL_Texture *texture;

void audio_callback(void *userdata, Uint8 *stream, int len);
void packet_queue_init(PacketQueue *q);
int packet_queue_put(PacketQueue *q, AVPacket *pkt);
int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block);
int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size);
int refresh_video_timer(void *udata);
SDL_Thread *timer_thread = NULL;

int main(int argc, char *argv[])
{
  int ret = 0;
  AVFormatContext *fmt_ctx = NULL;
  char *in_file;
  int video_stream_index = -1;
  int audio_stream_index = -1;
  AVCodec *video_codec = NULL, *audio_codec = NULL;
  AVCodecParameters *audio_codecpar = NULL, *video_codecpar = NULL;
  AVCodecContext *audio_codec_ctx = NULL, *video_codec_ctx = NULL;
  SDL_AudioSpec wanted_spec, src_spec;
  int64_t in_channel_layout;
  int64_t out_channel_layout;
  AVPacket packet;
  SDL_Event event;

  av_log_set_level(AV_LOG_INFO);

  if (argc < 2)
  {
    av_log(NULL, AV_LOG_ERROR, "need infile\n");
    return -1;
  }
  in_file = argv[1];
  //初始化SDL
  ret = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER);
  if (ret != 0)
  {
    av_log(NULL, AV_LOG_ERROR, "sdl init error\n");
    return ret;
  }

  //获取输入文件上下文
  ret = avformat_open_input(&fmt_ctx,
                            in_file,
                            NULL,
                            NULL);
  if (!fmt_ctx)
  {
    av_log(NULL, AV_LOG_ERROR, "open input file fail\n");
    goto __FAIL;
  }

  //获取视频流索引和音频流索引
  ret = avformat_find_stream_info(fmt_ctx, NULL);
  if (ret < 0)
  {
    av_log(NULL, AV_LOG_ERROR, "find stream fail\n");
    goto __FAIL;
  }
  av_dump_format(fmt_ctx, 0, in_file, 0);
  video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, -1);
  audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_codec, -1);
  if (video_stream_index < 0 || audio_stream_index < 0)
  {
    av_log(NULL, AV_LOG_ERROR, "find audio or video  fail\n");
    goto __FAIL;
  }
  if (!audio_codec || !video_codec)
  {
    ret = -1;
    av_log(NULL, AV_LOG_ERROR, "find audio codec or video codec fail");
    goto __FAIL;
  }
  //获取解码器上下文并打开解码器
  audio_codecpar = fmt_ctx->streams[audio_stream_index]->codecpar;
  out_nb_samples = audio_codecpar->frame_size;

  video_codecpar = fmt_ctx->streams[video_stream_index]->codecpar;
  w_width = video_codecpar->width;
  w_height = video_codecpar->height;

  audio_codec_ctx = avcodec_alloc_context3(audio_codec);
  video_codec_ctx = avcodec_alloc_context3(video_codec);

  avcodec_parameters_to_context(audio_codec_ctx, audio_codecpar);
  avcodec_parameters_to_context(video_codec_ctx, video_codecpar);

  ret = avcodec_open2(audio_codec_ctx, audio_codec, NULL);
  if (ret < 0)
  {
    av_log(NULL, AV_LOG_ERROR, "open audio codec fail\n");
    goto __FAIL;
  }

  ret = avcodec_open2(video_codec_ctx, video_codec, NULL);
  if (ret < 0)
  {
    av_log(NULL, AV_LOG_ERROR, "open video codec fail\n");
    goto __FAIL;
  }

  //设置SDL_音频参数
  wanted_spec.freq = sample_rate;     //采样率
  wanted_spec.format = AUDIO_S16LSB;  //采样大小
  wanted_spec.channels = out_channel; //声道数
  wanted_spec.silence = 0;
  wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; //采样个数
  wanted_spec.callback = audio_callback;
  wanted_spec.userdata = audio_codec_ctx;

  ret = SDL_OpenAudio(&wanted_spec, &src_spec);
  if (ret < 0)
  {
    av_log(NULL, AV_LOG_ERROR, "open audio device fail\n");
    goto __FAIL;
  }

  //设置重采样
  in_channel_layout = audio_codecpar->channel_layout;
  out_channel_layout = av_get_default_channel_layout(out_channel);
  audio_convert_ctx = swr_alloc();
  if (!audio_convert_ctx)
  {
    ret = -1;
    av_log(NULL, AV_LOG_ERROR, "alloc  swr fail\n");
    goto __FAIL;
  }

  swr_alloc_set_opts(audio_convert_ctx,
                     out_channel_layout,
                     out_format,
                     sample_rate,
                     in_channel_layout,
                     audio_codecpar->format,
                     audio_codecpar->sample_rate,
                     0,
                     NULL);

  swr_init(audio_convert_ctx);
  SDL_PauseAudio(0);

  //初始化渲染
  win = SDL_CreateWindow("hello world",
                         100,
                         100,
                         w_width,
                         w_height,
                         SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
  if (!win)
  {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create window!");
    goto __FAIL;
  }
  renderer = SDL_CreateRenderer(win,
                                -1,
                                0);
  if (!renderer)
  {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer!");
    goto __FAIL;
  }

  int pixformat = SDL_PIXELFORMAT_IYUV;
  texture = SDL_CreateTexture(renderer,
                              pixformat,
                              SDL_TEXTUREACCESS_STREAMING,
                              w_width,
                              w_height);

  if (!texture)
  {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create Texture!");
    goto __FAIL;
  }
  sws_ctx = sws_getContext(w_width,
                           w_height,
                           video_codecpar->format,
                           w_width,
                           w_height,
                           out_yuv_foramt,
                           SWS_BILINEAR,
                           NULL,
                           NULL,
                           NULL);

  yuv_frame = av_frame_alloc();
  yuv_frame->width = w_width;
  yuv_frame->height = w_height;
  yuv_frame->format = out_yuv_foramt;
  av_frame_get_buffer(yuv_frame, 32);
  timer_thread = SDL_CreateThread(refresh_video_timer,
                                  NULL,
                                  video_codec_ctx);

  packet_queue_init(&audioq);
  packet_queue_init(&videoq);

  while (av_read_frame(fmt_ctx, &packet) >= 0)
  {
    if (packet.stream_index == audio_stream_index)
    {
      packet_queue_put(&audioq, &packet);
    }
    else if (packet.stream_index == video_stream_index)
    {
      packet_queue_put(&videoq, &packet);
    }
    else
    {
      av_packet_unref(&packet);
    }
    SDL_PollEvent(&event);
    switch (event.type)
    {
    case SDL_QUIT:
      quit = 1;
      goto __FAIL;
      break;
    default:
      break;
    }
  }
  while (1)
  {
    SDL_WaitEvent(&event);
    switch (event.type)
    {
    case SDL_QUIT:
      quit = 1;
      goto __FAIL;
      break;
    default:
      break;
    }
  }

__FAIL:
  SDL_CloseAudio();
  SDL_Quit();
  if (audio_codecpar)
  {
    avcodec_parameters_free(&audio_codecpar);
  }

  if (video_codecpar)
  {
    avcodec_parameters_free(&video_codecpar);
  }

  if (video_codec_ctx)
  {
    avcodec_close(video_codec_ctx);
    avcodec_free_context(&video_codec_ctx);
  }

  if (audio_codec_ctx)
  {
    avcodec_close(audio_codec_ctx);
    avcodec_free_context(&audio_codec_ctx);
  }
  if (fmt_ctx)
  {
    avformat_close_input(&fmt_ctx);
    avformat_free_context(fmt_ctx);
  }

  return ret;
}

void audio_callback(void *userdata, Uint8 *stream, int len)
{

  AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
  int len1, audio_size;

  static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
  static unsigned int audio_buf_size = 0;
  static unsigned int audio_buf_index = 0;

  while (len > 0)
  {
    if (audio_buf_index >= audio_buf_size)
    {
      /* We have already sent all our data; get more */
      audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));
      if (audio_size < 0)
      {
        /* If error, output silence */
        audio_buf_size = 1024; // arbitrary?
        memset(audio_buf, 0, audio_buf_size);
      }
      else
      {
        audio_buf_size = audio_size;
      }
      audio_buf_index = 0;
    }
    len1 = audio_buf_size - audio_buf_index;
    if (len1 > len)
      len1 = len;
    fprintf(stderr, "index=%d, len1=%d, len=%d\n",
            audio_buf_index,
            len,
            len1);
    memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);
    len -= len1;
    stream += len1;
    audio_buf_index += len1;
  }
}

void packet_queue_init(PacketQueue *q)
{
  memset(q, 0, sizeof(PacketQueue));
  q->mutex = SDL_CreateMutex();
  q->cond = SDL_CreateCond();
}

int packet_queue_put(PacketQueue *q, AVPacket *pkt)
{

  AVPacketList *pkt1;
  AVPacket dst_pkt;
  av_init_packet(&dst_pkt);
  if (av_packet_ref(&dst_pkt, pkt) < 0)
  {
    return -1;
  }
  pkt1 = av_malloc(sizeof(AVPacketList));
  if (!pkt1)
    return -1;
  pkt1->pkt = dst_pkt;
  pkt1->next = NULL;

  SDL_LockMutex(q->mutex);

  if (!q->last_pkt)
  {
    q->first_pkt = pkt1;
  }
  else
  {
    q->last_pkt->next = pkt1;
  }

  q->last_pkt = pkt1;
  q->nb_packets++;
  q->size += pkt1->pkt.size;
  SDL_CondSignal(q->cond);

  SDL_UnlockMutex(q->mutex);
  return 0;
}

int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
  AVPacketList *pkt1;
  int ret;

  SDL_LockMutex(q->mutex);

  for (;;)
  {

    if (quit)
    {
      ret = -1;
      break;
    }

    pkt1 = q->first_pkt;
    if (pkt1)
    {
      q->first_pkt = pkt1->next;
      if (!q->first_pkt)
        q->last_pkt = NULL;
      q->nb_packets--;
      q->size -= pkt1->pkt.size;
      *pkt = pkt1->pkt;
      av_free(pkt1);
      ret = 1;
      break;
    }
    else if (!block)
    {
      ret = 0;
      break;
    }
    else
    {
      SDL_CondWait(q->cond, q->mutex);
    }
  }
  SDL_UnlockMutex(q->mutex);
  return ret;
}

int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)
{

  static AVPacket pkt;
  static uint8_t *audio_pkt_data = NULL;
  static int audio_pkt_size = 0;
  static AVFrame frame;
  av_frame_get_buffer(&frame, 0);
  int len1, data_size = 0;
  int ret = 0;
  int is_no_pkt = 0;

  for (;;)
  {

    if (pkt.data)
      av_packet_unref(&pkt);

    if (quit)
    {
      return -1;
    }

    if (packet_queue_get(&audioq, &pkt, 1) < 0)
    {
      ret = avcodec_send_packet(aCodecCtx, NULL);
      if (ret < 0)
      {
        return -1;
      }
      is_no_pkt = 1;
      goto __RECEIVE;
    }

    ret = avcodec_send_packet(aCodecCtx, &pkt);
    if (ret < 0)
    {
      ret = -1;
      printf("decode error");
      av_packet_unref(&pkt);
      return -1;
    }
  __RECEIVE:
    if (is_no_pkt)
    {
      return -1;
    }
    else if (pkt.data)
    {
      av_packet_unref(&pkt);
    }
    ret = avcodec_receive_frame(aCodecCtx, &frame);
    if (ret < 0)
    {
      continue;
    }

    data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;

    swr_convert(audio_convert_ctx,
                &audio_buf,
                SDL_AUDIO_BUFFER_SIZE,
                (const uint8_t **)frame.data,
                frame.nb_samples);
    return data_size;
  }
}

int video_decode_frame(AVCodecContext *vCodecCtx, uint8_t *buffer)
{

  static AVPacket pkt;
  static uint8_t *audio_pkt_data = NULL;
  static int audio_pkt_size = 0;
  static AVFrame frame;
  av_frame_get_buffer(&frame, 0);
  int len1, data_size = 0;
  int ret = 0;
  int is_no_pkt = 0;

  for (;;)
  {

    if (pkt.data)
      av_packet_unref(&pkt);

    if (quit)
    {
      return -1;
    }

    if (packet_queue_get(&videoq, &pkt, 1) < 0)
    {
      ret = avcodec_send_packet(vCodecCtx, NULL);
      if (ret < 0)
      {
        return -1;
      }
      is_no_pkt = 1;
      goto __RECEIVE;
    }

    ret = avcodec_send_packet(vCodecCtx, &pkt);
    if (ret < 0)
    {
      ret = -1;
      printf("decode error");
      av_packet_unref(&pkt);
      return -1;
    }
  __RECEIVE:
    if (is_no_pkt)
    {
      return -1;
    }
    else if (pkt.data)
    {
      av_packet_unref(&pkt);
    }
    ret = avcodec_receive_frame(vCodecCtx, &frame);
    if (ret < 0)
    {
      continue;
    }

    sws_scale(sws_ctx,
              (const uint8_t **)frame.data,
              frame.linesize,
              0,
              w_height,
              yuv_frame->data,
              yuv_frame->linesize);
    int y_size = w_width * w_height;
    memcpy(buffer, yuv_frame->data[0], y_size);
    memcpy(buffer + y_size, yuv_frame->data[1], y_size / 4);
    memcpy(buffer + y_size + y_size / 4, yuv_frame->data[2], y_size / 4);
    return 0;
  }
}

int refresh_video_timer(void *udata)
{
  AVCodecContext *video_context = udata;
  size_t buffer_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, w_width, w_height, 32);

  uint8_t *buffer = malloc(buffer_size);
  SDL_Rect rect;

  while (!quit)
  {
    int ret = video_decode_frame(video_context, buffer);
    if (ret == 0)
    {
      SDL_UpdateTexture(texture, NULL, buffer, w_width);
      rect.x = 0;
      rect.y = 0;
      rect.w = w_width;
      rect.h = w_height;

      SDL_RenderClear(renderer);
      SDL_RenderCopy(renderer, texture, NULL, &rect);
      SDL_RenderPresent(renderer);
      SDL_Delay(1000/35);
    }
    else
    {
      break;
    }
  }

  if (buffer)
  {
    free(buffer);
  }

  return 0;
}
